<template>
  <a-modal
    :zIndex="zIndex"
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    width="850px"
    :cancelText="$t('关闭')"
    :confirmLoading="confirmLoading"
    v-drag
  >
    <template slot="footer">
      <a-row :gutter="24">
        <a-col :span="12">
          <div style="text-align: left" v-if="!dataForm.id && !isMeal">
            <a-checkbox v-model="isContinue" @change="changeAddState"></a-checkbox> {{ $t('连续添加') }}
          </div>
        </a-col>
        <a-col :span="12">
          <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('取消') }}</a-button>
          <a-button type="primary" @click="handleOk" v-if="!disable" :disabled="confirmLoading">{{
            $t('保存')
          }}</a-button>
        </a-col>
      </a-row>
    </template>
    <div style="height: 500px; overflow-y: auto">
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model
            ref="dataForm"
            :rules="rules"
            :model="dataForm"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            v-enterToNext
          >
            <a-row>
              <div style="margin: 0 0 10px -10px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('基本信息') }}
              </div>
              <a-col :span="12">
                <a-form-model-item
                  prop="supplierId"
                  style="margin-bottom: 10px"
                  :label="$t('所属商户')"
                  v-if="stateDisable == 10"
                >
                  <a-select v-model="dataForm.supplierId">
                    <a-select-option value="">{{ $t('请选择') }}</a-select-option>
                    <a-select-option
                      v-for="(item, index) in supplierList"
                      :key="index"
                      :value="item.id"
                      :disabled="disable"
                      >{{ item.name }}</a-select-option
                    >
                  </a-select>
                </a-form-model-item>
                <a-form-model-item prop="code" style="margin-bottom: 10px" :label="$t('商品编码')">
                  <a-input v-model="dataForm.code" ref="code" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="name" style="margin-bottom: 10px" :label="$t('商品名称')">
                  <a-input v-model="dataForm.name" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="kind" style="margin-bottom: 10px" :label="$t('商品分类')">
                  <a-tree-select
                    show-search
                    :filterTreeNode="filterTreeNode"
                    :disabled="disable"
                    allowClear
                    v-model="dataForm.kind"
                    :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                    :tree-data="treeData"
                    :placeholder="$t('请选择')"
                  />
                </a-form-model-item>
                <a-form-model-item style="margin-bottom: 10px" :label="$t('供应商')">
                  <a-select v-model="dataForm.supplierId">
                    <a-select-option value="">{{ $t('请选择') }}</a-select-option>
                    <a-select-option
                      v-for="(item, index) in supplierList"
                      :key="index"
                      :value="item.id"
                      :disabled="disable"
                      >{{ item.name }}</a-select-option
                    >
                  </a-select>
                </a-form-model-item>
                <a-form-model-item
                  prop="barCode"
                  style="margin-bottom: 10px"
                  :label="$t('条形码')"
                >
                  <a-input v-model="dataForm.barCode" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item
                  prop="unit"
                  style="margin-bottom: 10px"
                  :label="$t('单位')"
                >
                  <a-select
                    :placeholder="$t('请选择')"
                    v-model="dataForm.unit"
                    showSearch
                    :filter-option="false"
                    @search="onSearch"
                    :disabled="disable"
                  >
                    <div slot="dropdownRender" slot-scope="menu">
                      <div
                        style="padding: 4px 8px; cursor: pointer"
                        @mousedown="(e) => e.preventDefault()"
                        @click="addUnit"
                      >
                        <a-icon type="plus" /> {{ $t('添加') }}
                      </div>
                      <a-divider style="margin: 4px 0 0" />
                      <v-nodes :vnodes="menu" />
                    </div>
                    <a-select-option v-for="(item, i) in unitList" :key="i" :value="item.id">{{
                      item.name
                    }}</a-select-option>
                  </a-select>
                </a-form-model-item>
                <a-form-model-item
                  prop="standard"
                  style="margin-bottom: 10px"
                  :label="$t('规格')"
                >
                  <a-input v-model="dataForm.standard" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item
                  prop="cost"
                  style="margin-bottom: 10px"
                  :label="$t('参考成本')"
                  v-if="stateDisable != 10"
                >
                  <a-input v-model="dataForm.cost" :disabled="disable"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item
                  prop="allowOuts"
                  style="margin-bottom: 5px"
                >
                  <a-checkbox v-model="dataForm.allowOuts" ref="allowOuts" :disabled="disable">{{
                    $t('0库存允许出库')
                  }}</a-checkbox>
                </a-form-model-item>
                <a-row>
                  <a-col :span="8">
                    <a-form-model-item
                      :labelCol="{ span: 0 }"
                      :wrapperCol="{ span: 24 }"
                      prop="allowWarns"
                      style="margin-bottom: 5px"
                    >
                      <a-checkbox
                        v-model="dataForm.allowWarns"
                        ref="allowWarns"
                        :disabled="disable"
                        @click="handleAllowWarns(dataForm.allowWarns)"
                        >{{ $t('缺货提醒') }}</a-checkbox
                      >
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="14">
                    <a-form-model-item
                      prop="warnNum"
                      :labelCol="{ span: 14 }"
                      :wrapperCol="{ span: 10 }"
                      style="margin-bottom: 5px"
                      :label="$t('提醒数量')"
                    >
                      <a-input v-model="dataForm.warnNum" ref="warnNum" :disabled="allowWarnsdisable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-form-model-item
                  prop="verifyType"
                  :label="$t('核销方式')"
                  :labelCol="{ span: 4 }"
                  :wrapperCol="{ span: 20 }"
                >
                  <a-radio-group v-model="dataForm.verifyType" :disabled="disable">
                    <a-radio style="line-height: 40px" :value="item.value" v-for="(item, index) in list" :key="index">{{
                      item.title
                    }}</a-radio>
                  </a-radio-group>
                </a-form-model-item>
                <a-col :span="24" style="padding-right: 0">
                  <a-form-model-item
                    prop="picture"
                    :label="$t('图片')"
                    :labelCol="{ span: 3 }"
                    :wrapperCol="{ span: 18 }"
                  >
                    <div>
                      <j-upload v-model="dataForm.picture" accept="jpg,jpeg,png,gif,webp" fileType="image" :count="1" :disabled="disable"></j-upload>
                      <div style="color: #f7b926; line-height: 1.2">
                        {{ $t('建议尺寸：750*750，大小不要超过500KB') }}
                      </div>
                    </div>
                  </a-form-model-item>
                </a-col>
              </a-col>
            </a-row>
            
            
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('回收规则设置') }}
              </div>
              <a-col :span="12">
                <a-row v-if="stateDisable != 2">
                  <a-col :span="12">
                    <a-form-model-item prop="allowCoins" style="margin-bottom: 5px">
                      <a-checkbox v-model="dataForm.allowCoins" :disabled="disable">{{ $t('允许币回收') }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="coin" style="margin-bottom: 5px" :label="$t('代币')">
                      <a-input v-model="dataForm.coin" :disabled="disable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowLotterys" style="margin-bottom: 5px" label="" label-width="50px">
                      <a-checkbox v-model="dataForm.allowLotterys" ref="allowLotterys" :disabled="disable">{{
                        $t('允许彩票回收')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="lottery" :label="$t('彩票')" style="margin-bottom: 5px">
                      <a-input v-model="dataForm.lottery" ref="lottery" :disabled="disable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowPointss" style="margin-bottom: 5px">
                      <a-checkbox
                        v-model="dataForm.allowPointss"
                        ref="allowPointss"
                        :disabled="disable"
                        style="width: 110%"
                        >{{ $t('允许A币回收') }}</a-checkbox
                      >
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="points" style="margin-bottom: 5px" :label="$t('A币')">
                      <a-input v-model="dataForm.points" :disabled="disable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
              <a-col :span="12">
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowIntegrals" style="margin-bottom: 5px">
                      <a-checkbox v-model="dataForm.allowIntegrals" :disabled="disable">{{
                        $t('允许积分回收')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="integral" style="margin-bottom: 5px" :label="$t('积分')">
                      <a-input v-model="dataForm.integral" ref="integral" :disabled="disable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item
                      prop="allowAlotterys"
                      :labelCol="{ span: 8 }"
                      :wrapperCol="{ span: 16 }"
                      style="margin-bottom: 5px"
                    >
                      <a-checkbox v-model="dataForm.allowAlotterys" ref="allowAlotterys" :disabled="disable">{{
                        $t('允许A票回收')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="alottery" style="margin-bottom: 5px" :label="$t('A票')">
                      <a-input v-model="dataForm.alottery" ref="alottery" :disabled="disable"></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('更多设置') }}
              </div>
              <a-row>
                <a-col :span="9">
                  <a-form-model-item prop="state" style="margin-bottom: 5px" :label="$t('上架状态')">
                    <a-radio-group v-model="dataForm.state" ref="state" :disabled="disable">
                      <a-radio value="Y">{{ $t('上架') }}</a-radio>
                      <a-radio value="N">{{ $t('下架') }}</a-radio>
                    </a-radio-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    prop="comment"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    :label="$t('商品简介')"
                    style="margin-left: 4px"
                  >
                    <a-textarea v-model="dataForm.comment" ref="comment" :rows="2" :disabled="disable" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row v-if="stateDisable != 11">
                <a-col :span="24">
                  <a-form-model-item
                    prop="details"
                    :label="$t('商品详情')"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-left: 4px"
                  >
                    <div style="color: #e28909">
                      {{
                        $t('用于小程序商品详情的展示；建议图片宽度375px，上传多张图片时，每张图片的宽度应该保持一致。')
                      }}
                    </div>
                    <j-upload
                      v-model="dataForm.details"
                      accept="jpg,jpeg,png,gif,webp"
                      fileType="image"
                      :disabled="disable"
                      class="detail-upload"
                      text="upload"
                    ></j-upload>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-row>
          </a-form-model>
        </a-col>
      </a-row>
    </div>
    <add-unit-modal ref="unitModal" @ok="updateUnit"></add-unit-modal>
  </a-modal>
</template>

<script>
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { httpAction, getAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import AddUnitModal from './AddUnitModal'
let that
export default {
  name: 'RecyclingGiftModal',
  components: {
    AddUnitModal,
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes,
    },
  },
  data() {
    return {
      zIndex: 1001,
      title: '',
      confirmLoading: false,
      dataForm: {
        code: '',
        name: '',
        allowOuts: false,
        allowWarns: '',
        warnNum: 0,
        verifyType: '1',
        allowMemberDiscounts: false,
        allowCoins: '',
        allowLotterys: '',
        allowPointss: '',
        allowSales: '',
        allowIntegrals: '',
        allowAlotterys: '',
        authSale: '',
        authSales: false,
        state: '',
        allowMemberType: '',
        allowDeviceType: '',
        allowDevice: '',
        form: '', //形态
        bisGoodsPackageList: [],
        validityType: '0',
      },
      treeData: [],
      deviceTypeShow: false,
      deviceShow: false,
      memberShow: false,
      visible: false,
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 15 },
      },
      rules: {
        supplierId: [{ required: true, message: this.$t('请选择') }],
        name: [{ required: true, message: this.$t('请输入卖品名称') }],
        kind: [{ required: true, message: this.$t('请选择卖品分类') }],
        cost: [
          { required: true, message: this.$t('请输入卖品参考成本') },
          { pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') },
        ],
        realCoin2: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        presentCoin2: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        price: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        integral: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        lottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        alottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        points: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        realCoin8: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        presentCoin8: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        referencePrice: [{ required: true, message: this.$t('请输入店面售价') }],
        cost1: [{ required: true, message: this.$t('请输入结算价') }],
      },
      dataSource: [],
      memberList: [],
      deviceTypeList: [],
      deviceList: [],
      url: {
        add: '/goods/bisGoodsInfor/add',
        edit: '/goods/bisGoodsInfor/edit',
        importExcelUrl: '/sys/common/upload',
      },
      disable: false,
      limitCycledisable: false,
      allowWarnsdisable: true,
      list: [],
      allowMemberType: '',
      allowDevice: '',
      allowDeviceType: '',
      stateDisable: '',

      goodsFormList: [],
      addGoodsListTwo: [],
      supplierList: [], //供应商

      //现调
      addRawList: [],
      goodsPriceRule: {
        isGoodsPriceRule: 0,
        coinRule: 1,
        lotteryRule: 1,
        integralRule: 1,
        pointsRule: 1,
        alotteryRule: 1,
      },
      levelList: [],
      elevation: [],
      chargeAfter: false,
      renewalList: [],
      renewalAfter: false,
      unitList: [],
      isContinue: true,
      isMeal: false,
      tenantid: Vue.ls.get(TENANT_ID),
    }
  },
  created() {
    that = this
  },
  methods: {
    changePrice() {
      if (that.goodsPriceRule.isGoodsPriceRule == 1) {
        that.$set(that.dataForm, 'coin', that.dataForm.price * that.goodsPriceRule.coinRule)
        that.$set(that.dataForm, 'lottery', that.dataForm.price * that.goodsPriceRule.lotteryRule)
        that.$set(that.dataForm, 'integral', that.dataForm.price * that.goodsPriceRule.integralRule)
        that.$set(that.dataForm, 'points', that.dataForm.price * that.goodsPriceRule.pointsRule)
        that.$set(that.dataForm, 'alottery', that.dataForm.price * that.goodsPriceRule.alotteryRule)
      }
    },
    getInfo() {
      getAction('/bis/bisSysParameter/list', { name: 'isGoodsPriceRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.goodsPriceRule.isGoodsPriceRule = res.result.records[0].ivalue
            if (that.goodsPriceRule.isGoodsPriceRule == 1) {
              getAction('/bis/bisSysParameter/list', {
                name: 'coinRule,lotteryRule,integralRule,pointsRule,alotteryRule',
              }).then((res) => {
                if (res.success) {
                  if (res.result.total > 0) {
                    res.result.records.forEach((el) => {
                      if (el.name == 'coinRule') {
                        that.goodsPriceRule.coinRule = el.dvalue
                      }
                      if (el.name == 'lotteryRule') {
                        that.goodsPriceRule.lotteryRule = el.dvalue
                      }
                      if (el.name == 'integralRule') {
                        that.goodsPriceRule.integralRule = el.dvalue
                      }
                      if (el.name == 'pointsRule') {
                        that.goodsPriceRule.pointsRule = el.dvalue
                      }
                      if (el.name == 'alotteryRule') {
                        that.goodsPriceRule.alotteryRule = el.dvalue
                      }
                    })
                  }
                } else {
                  that.$message.error(res.message)
                }
              })
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    add(value, arg) {
      that.visible = true
      that.isContinue = localStorage.getItem('goodsAddInfo')
        ? JSON.parse(localStorage.getItem('goodsAddInfo')).state
        : true
      if (arg) {
        that.isMeal = true
      } else {
        that.isMeal = false
      }
      if (value == 19) {
        that.dataForm = {
          code: '',
          name: '',
          allowOut: 'N',
          allowWarns: '',
          warnNum: 0,
          verifyType: '1',
          cost: '',
          allowMemberDiscount: 'N',
          allowCoins: '',
          allowLotterys: '',
          allowPointss: '',
          allowSale: 'N',
          allowIntegrals: '',
          allowAlotterys: '',
          authSale: '',
          authSales: false,
          state: 'Y',
          limitCycle: '0',
          allowMemberType: '',
          allowDevices: [],
          allowDevice: '',
          allowDeviceTypes: [],
          allowDeviceType: '',
          form: value,
          species: '1',
          bisGoodsPackageList: [],
          supplierId: '',
        }
      }
      that.edit(that.dataForm)
    },
    edit(record, state) {
      if (record.id) {
        that.isMeal = false
      }
      that.getInfo()
      that.stateDisable = record.form
      that.disable = state
      that.getMemberList()
      that.getDeviceTypeList()
      that.visible = true
      that.select()
      if (that.disable == true || record.limitCycle == '0') {
        that.limitCycledisable = true
      } else {
        that.limitCycledisable = false
      }
      if (record.allowWarn == 'Y' || that.disable == true) {
        that.allowWarnsdisable = true
      }
      that.onSearch()
      initDictOptions('bis_goods_infor.verify_type').then((res) => {
        if (res.success) {
          that.list = res.result
        }
      })
      that.getSupplier(0)
      if (record.allowMemberDiscount == 'Y') {
        record.allowMemberDiscounts = true
      } else {
        record.allowMemberDiscounts = false
      }
      if (record.allowOut == 'Y') {
        record.allowOuts = true
      } else {
        record.allowOuts = false
      }
      if (record.allowWarn == 'Y') {
        record.allowWarns = true
      } else {
        record.allowWarns = false
      }
      if (record.allowCoin == 'Y') {
        record.allowCoins = true
      } else {
        record.allowCoins = false
      }
      if (record.allowLottery == 'Y') {
        record.allowLotterys = true
      } else {
        record.allowLotterys = false
      }
      if (record.allowPoints == 'Y') {
        record.allowPointss = true
      } else {
        record.allowPointss = false
      }
      if (record.allowSale == 'Y') {
        record.allowSales = true
      } else {
        record.allowSales = false
      }
      if (record.allowIntegral == 'Y') {
        record.allowIntegrals = true
      } else {
        record.allowIntegrals = false
      }
      if (record.allowAlottery == 'Y') {
        record.allowAlotterys = true
      } else {
        record.allowAlotterys = false
      }
      if (record.authSale == '1') {
        record.authSales = true
      } else {
        record.authSales = false
      }

      that.allowMemberType = record.allowMemberType
      if (that.allowMemberType == '' || that.allowMemberType == null || that.allowMemberType == undefined) {
        that.memberShow = false
      } else {
        record.allowMemberTypes = record.allowMemberType.split(',')
        that.allowMemberType = 'part'
        that.memberShow = true
      }

      that.allowDeviceType = record.allowDeviceType
      if (that.allowDeviceType == null || that.allowDeviceType == undefined) {
        that.allowDeviceType = ''
      }
      if (that.allowDeviceType == '') {
        that.deviceTypeShow = false
      } else {
        record.allowDeviceTypes = record.allowDeviceType.split(',')
        that.allowDeviceType = 'part'
        that.deviceTypeShow = true
      }

      that.allowDevice = record.allowDevice
      if (that.allowDevice == '' || that.allowDevice == null || that.allowDevice == undefined) {
        that.deviceShow = false
      } else {
        record.allowDevices = record.allowDevice.split(',')
        that.allowDevice = 'part'
        that.deviceShow = true
      }
      that.dataForm = Object.assign({}, record)
      if (that.dataForm.id && (record.form == 2 || record.form == 3 || record.form == 4)) {
        that.getGoodsMemberTypeList()
        that.getRenewal()
      }
      that.$set(that.dataForm, 'realCoin2', that.dataForm.realCoin)
      that.$set(that.dataForm, 'realCoin8', that.dataForm.realCoin)
      that.$set(that.dataForm, 'presentCoin2', that.dataForm.presentCoin)
      that.$set(that.dataForm, 'presentCoin8', that.dataForm.presentCoin)
      that.$set(that.dataForm, 'cost1', that.dataForm.cost)
      that.getDeviceList()
    },
    getRenewal() {
      getAction('/goods/bisGoodsRenewal/list', { goodsId: that.dataForm.id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.renewalList = res.result.records
          } else {
            that.renewalList = [
              {
                type: '0',
                renewal: '',
              },
            ]
          }
        }
      })
    },
    getLevels() {
      that.levelList = []
      getAction('/member/bisMemberRule/list', { notNonmember: 1, manageState: 'Y', pageSize: 40 }).then((res) => {
        if (res.success) {
          that.levelList = res.result.records
          return
        }
      })
    },
    getGoodsMemberTypeList() {
      getAction('/goods/bisGoodsMemberType/list', { goodsId: that.dataForm.id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            res.result.records.forEach((rs) => {
              rs.disable = true
            })
            that.elevation = res.result.records
          } else {
            that.elevation = [
              {
                memberType: undefined,
                memberTypeAfter: undefined,
                disable: false,
              },
            ]
          }
        }
      })
    },
    getSupplier(type) {
      getAction('/goods/bisGoodsSupplier/list', { type: type, pageSize: 50 }).then((res) => {
        if (res.success) {
          that.supplierList = res.result.records
        }
      })
    },
    filterTreeNode(inputValue, treeNode) {
      // 假设输入格式为 "级别:关键词" 如 "2:电器"
      const match = inputValue.match(/([123]):(.+)/)
      if (match) {
        const level = parseInt(match[1])
        const keyword = match[2]
        const nodeLevel = this.getNodeLevel(treeNode)
        
        if (nodeLevel !== level) return false
        return treeNode.data.props.title.includes(keyword)
      }
      
      // 默认搜索
      return treeNode.data.props.title.includes(inputValue)
    },
    getNodeLevel(node) {
      let level = 1
      let parent = node.parent
      while (parent) {
        level++
        parent = parent.parent
      }
      return level
    },
    select() {
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      if(!that.disable && !that.tenantid){
        param.tenantId = 0
      }
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, that.$t('提示'), {
            confirmButtonText: that.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      if(!that.visible && !that.tenantid){
        query.tenantId = 0
      }
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    getGoodsListTwo(form, value) {
      let querydata = {}
      querydata.pageIndex = 1
      querydata.pageSize = 10
      querydata.form = form
      querydata.goodsInfo = value
      getAction('/goods/bisGoodsInfor/list', querydata).then((res) => {
        if (res.success) {
          that.addRawList = res.result.records
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let total = {}
      myArr.forEach((col, index) => {
        if (index == 0) {
          total[col] = that.$t('合计')
        } else {
          total[col] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach((ds) => {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      that.dataForm.cost = total.amountCost
      dataSource.push(total)
    },
    changelimitCycleState(e) {
      if (e == '0') {
        that.limitCycledisable = true
      } else {
        that.limitCycledisable = false
      }
    },
    handleAllowWarns(e) {
      if (e) {
        that.allowWarnsdisable = true
      } else {
        that.allowWarnsdisable = false
      }
    },
    getTypeValue(e) {
      if (e.target.value == 'part') {
        that.memberShow = true
      } else {
        that.memberShow = false
      }
    },
    getDeviceTypeValue(e) {
      if (e.target.value == 'part') {
        that.deviceTypeShow = true
      } else {
        that.deviceTypeShow = false
      }
    },
    getDeviceValue(e) {
      if (e.target.value == 'part') {
        that.deviceShow = true
      } else {
        that.deviceShow = false
      }
    },
    getMemberList() {
      getAction('/member/bisMemberRule/list').then((res) => {
        if (res.success) {
          that.memberList = res.result.records
        }
      })
    },
    getDeviceTypeList() {
      getAction('/device/bisDeviceInfo/getTypeList', { kind: 0 }).then((res) => {
        if (res.success) {
          that.deviceTypeList = res.result.records
        }
      })
    },
    getDeviceList() {
      let kind = 0
      let type = ''
      if (that.allowDeviceType == 'part') {
        if (that.dataForm.allowDeviceTypes) {
          type = that.dataForm.allowDeviceTypes.join(',')
        }
      }
      getAction('/device/bisDeviceInfo/list', {
        pageSize: 999,
        type: type,
        kind: kind,
        column: 'type',
        order: 'ASC',
      }).then((res) => {
        if (res.success) {
          that.deviceList = res.result.records
        }
      })
    },
    close() {
      that.$emit('close')
      that.visible = false
    },
    handleOk() {
      let querydata = {}
      that.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (that.dataForm.name == '') {
            that.$message.warn(that.$t('请输入商品名称'))
            return
          }
          if (that.dataForm.allowMemberDiscounts == true) {
            that.dataForm.allowMemberDiscount = 'Y'
          } else {
            that.dataForm.allowMemberDiscount = 'N'
          }
          if (that.dataForm.allowOuts == true) {
            that.dataForm.allowOut = 'Y'
          } else {
            that.dataForm.allowOut = 'N'
          }
          if (that.dataForm.allowWarns == true) {
            that.dataForm.allowWarn = 'Y'
          } else {
            that.dataForm.allowWarn = 'N'
          }
          if (that.dataForm.allowLotterys == true) {
            that.dataForm.allowLottery = 'Y'
            if (that.dataForm.lottery == undefined || that.dataForm.lottery === '') {
              that.$message.warn(that.$t('彩票值不能为空'))
              return
            }
          } else {
            that.dataForm.allowLottery = 'N'
          }
          if (that.dataForm.allowCoins == true) {
            that.dataForm.allowCoin = 'Y'
            if (that.dataForm.coin == undefined || that.dataForm.coin === '') {
              that.$message.warn(that.$t('代币值不能为空'))
              return
            }
          } else {
            that.dataForm.allowCoin = 'N'
          }
          if (that.dataForm.allowPointss == true) {
            that.dataForm.allowPoints = 'Y'
            if (that.dataForm.points == undefined || that.dataForm.points === '') {
              that.$message.warn(that.$t('A币不能为空'))
              return
            }
          } else {
            that.dataForm.allowPoints = 'N'
          }
          if (that.dataForm.allowSales == true) {
            that.dataForm.allowSale = 'Y'
            if (that.dataForm.price == undefined || that.dataForm.price === '') {
              that.$message.warn(that.$t('售价不能为空'))
              return
            }
          } else {
            that.dataForm.allowSale = 'N'
          }
          if (that.dataForm.allowIntegrals == true) {
            that.dataForm.allowIntegral = 'Y'
            if (that.dataForm.integral == undefined || that.dataForm.integral === '') {
              that.$message.warn(that.$t('积分不能为空'))
              return
            }
          } else {
            that.dataForm.allowIntegral = 'N'
          }
          if (that.dataForm.allowAlotterys == true) {
            that.dataForm.allowAlottery = 'Y'
            if (that.dataForm.alottery == undefined || that.dataForm.alottery === '') {
              that.$message.warn(that.$t('A票值不能为空'))
              return
            }
          } else {
            that.dataForm.allowAlottery = 'N'
          }
          if (that.dataForm.authSales == true) {
            that.dataForm.authSale = 1
          } else {
            that.dataForm.authSale = 0
          }
          if (that.dataForm.limitCycle != '0') {
            if (that.dataForm.limitNum == undefined || that.dataForm.limitNum === '') {
              that.$message.warn(that.$t('限购数量不能为空'))
              return
            }
          }
          if (that.allowMemberType == 'part') {
            if (that.dataForm.allowMemberTypes == null || that.dataForm.allowMemberTypes == '') {
              that.$message.warn(that.$t('请选择会员类型'))
              return
            }
            that.dataForm.allowMemberType = that.dataForm.allowMemberTypes.join(',')
          } else {
            that.dataForm.allowMemberType = ''
          }

          if (that.allowDeviceType == 'part') {
            if (that.dataForm.allowDeviceTypes == null || that.dataForm.allowDeviceTypes == '') {
              that.$message.warn(that.$t('请选择适用渠道'))
              return
            }
            that.dataForm.allowDeviceType = that.dataForm.allowDeviceTypes.join(',')
          } else {
            that.dataForm.allowDeviceType = ''
          }

          if (that.allowDevice == 'part') {
            if (that.dataForm.allowDevices == null || that.dataForm.allowDevices == '') {
              that.$message.warn(that.$t('请选择适用终端'))
              return
            }
            that.dataForm.allowDevice = that.dataForm.allowDevices.join(',')
          } else {
            that.dataForm.allowDevice = ''
          }
          querydata = that.dataForm
          let httpurl = ''
          let method = ''
          if (!that.dataForm.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          that.confirmLoading = true
          httpAction(httpurl, querydata, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok', res.result)
                if (that.dataForm.id || !that.isContinue || that.isMeal) {
                  that.close()
                } else {
                  that.initDataForm()
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      that.close()
    },
    changeChargeStatue(e) {
      that.chargeAfter = e.target.checked
    },
    changeRenewalStatue(e) {
      that.renewalAfter = e.target.checked
    },
    addElevation() {
      that.elevation.push({
        memberType: undefined,
        memberTypeAfter: undefined,
        disable: false,
      })
    },
    deleteElevation(i) {
      that.elevation.splice(i, 1)
    },
    editElevation(i) {
      that.$set(that.elevation[i], 'disable', false)
    },
    saveElevation(item, i) {
      if (item.memberType && item.memberTypeAfter) {
        that.$set(that.elevation[i], 'disable', true)
      } else if (!item.memberType) {
        that.$message.warn(that.$t('请选择晋级前会员类型'))
      } else {
        that.$message.warn(that.$t('请选择晋级后会员类型'))
      }
    },
    onSearch(value) {
      let query = {}
      query.state = 'Y'
      if (value) {
        query.name = ',' + value + ','
      }
      getAction('/goods/bisGoodsUnit/list', query).then((res) => {
        if (res.success) {
          that.unitList = res.result.records
        }
      })
    },
    addUnit() {
      that.$refs.unitModal.show()
    },
    updateUnit(res) {
      that.unitList = that.unitList.concat(res)
      that.$set(that.dataForm, 'unit', res.id)
    },
    changeAddState() {
      localStorage.setItem('goodsAddInfo', JSON.stringify({ state: that.isContinue }))
    },
    initDataForm() {
      let value = that.stateDisable
      if (value == 19) {
        that.dataForm = {
          code: '',
          name: '',
          allowOut: 'N',
          allowWarns: '',
          warnNum: 0,
          verifyType: '1',
          cost: '',
          allowMemberDiscount: 'N',
          allowCoins: '',
          allowLotterys: '',
          allowPointss: '',
          allowSale: 'N',
          allowIntegrals: '',
          allowAlotterys: '',
          authSale: '',
          authSales: false,
          state: 'Y',
          limitCycle: '0',
          allowMemberType: '',
          allowDevices: [],
          allowDevice: '',
          allowDeviceTypes: [],
          allowDeviceType: '',
          form: value,
          species: '1',
          bisGoodsPackageList: [],
          supplierId: '',
        }
      }
      that.allowWarnsdisable = true
      that.$refs.code.focus()
      that.edit(that.dataForm)
    },
  },
}
</script>
<style scoped>
.bold-font {
  font-weight: bold;
}
/deep/ .detail-upload .ant-upload-list-picture-card-container {
  width: 100%;
  height: 250px;
}
/deep/ .detail-upload .ant-upload-list-picture-card .ant-upload-list-item {
  width: 77% !important;
  height: 100% !important;
}
/deep/ .detail-upload .ant-upload .ant-upload-select .ant-upload-select-picture-card {
  width: 100%;
  height: 250px;
}
/deep/ .detail-upload .ant-upload {
  width: 77% !important;
  height: 100% !important;
}
</style>